﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IterationLife.System.Models;
using IterationLife.System.Services;
using Util.Exceptions;

namespace IterationLife.PlatformBackEnd.AppServices.Services.Implements
{
    public class AuthenticationService:IAuthenticationService
    {
        private ISignInManager _signInManager;
        public AuthenticationService(ISignInManager signInManager)
        {
            _signInManager = signInManager;
        }

        public string Login(string account, string password)
        {
            var result=_signInManager.PasswordSignIn(account, password);
            VerifySignInResult(result);
            return result.UserId;
        }

        private static void VerifySignInResult(SignInResult result)
        {
            if (!result.Succeeded)
                throw new Warning("用户名或密码错误");
            if (result.IsLockedOut)
                throw new Warning("账号被锁定");
            if (result.IsNotAllowed)
                throw new Warning("账号被禁用");
        }
    }
}
